package com.wdb007.venus.web.api;

import javax.servlet.Filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;

import com.wdb007.venus.web.api.filter.CheckPageParamFilter;
import com.wdb007.venus.web.api.filter.CheckSignFilter;
import com.wdb007.venus.web.api.filter.UserTokenFilter;

@SpringBootApplication
@ImportResource({ "classpath:applicationContext.xml" })
public class BootWebApiStart {
	private final static Logger logger = LoggerFactory.getLogger(BootWebApiStart.class);

	public static void main(String[] args) throws InterruptedException {
		long start = System.currentTimeMillis();
		new SpringApplicationBuilder().sources(BootWebApiStart.class).web(true) // 开启web服务
				.run(args);
		long end = System.currentTimeMillis();

		logger.info("venus-web启动成功,耗时：{} ms", end - start);
	}
	
	@Bean
	public FilterRegistrationBean filterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(userTokenFilter());
		registration.addUrlPatterns("/user/getUserInfo" , 
					"/user/getBorrowList" ,
					"/user/getBorrowingList" ,
					"/user/reportAbnormal" ,
					"/user/feedback" ,
					"/user/logout" ,
					"/bookgrid/unlock" ,
					"/bookgrid/queryStatus" ,
					"/bookgrid/borrowreturnv2" ,
					"/bookgrid/inventory",
					"/bookgrid/unlockOvertime",
//					"/bookgrid/queryPutonList" ,
//					"/bookgrid/queryBookList" ,
//					"/bookgrid/queryGridList" ,
//					"/bookgrid/queryGridBooks" ,
//					"/bookgrid/queryBookInfo",
//					"/bookgrid/queryBookRank",
					"/user/queryUserDepositAmount" , 
					"/charge/getExpenseRecord" ,
					"/charge/getChargeAmountList" , 
					"/charge/placeOrder" ,
					"/charge/queryPayStatus",
					"/charge/payment",
					"/charge/paymentVIPCard",
					"/charge/cancelPayment",
					"/charge/paymentV2",
//					"/charge/getOpenid",
					"/charge/refundDeposit",
					"/charge/getVIPAmountList",
					"/user/queryWishList",
					"/user/removeWishBook",
					"/user/saveWishBook",
					"/user/getIsbnWishFlag",
					"/user/queryRankList",
					"/user/queryUserRank",
					"/bookgrid/saveBookComments",
					"/bookgrid/getGridList",
					"/user/updateUserInfo",
					"/card/charge",
					"/user/getUser",
					"/user/addFamilyMember",
					"/user/updateFamilyMember"
					);
		registration.setName("userTokenFilter");
		registration.setOrder(1);
		return registration;
	}
	
	@Bean(name="userTokenFilter")
	public Filter userTokenFilter() {
		return new UserTokenFilter();
	}
	
	@Bean(name="checkPageFilter")
	public Filter checkPageFilter() {
		return new CheckPageParamFilter();
	}
	
	@Bean(name="checkSignFilter")
	public Filter checkSignFilter() {
		return new CheckSignFilter();
	}
	
	@Bean
	public FilterRegistrationBean checkSignFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(checkSignFilter());
		registration.addUrlPatterns("/*");
		return registration;
	}
	
	@Bean
	public FilterRegistrationBean checkPageFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(checkPageFilter());
		registration.addUrlPatterns(
					"/user/getBorrowList", 
					"/user/getBorrowingList",
					"/charge/getExpenseRecord",
					"/user/queryWishList",
					"/user/queryRankList",
					"/bookgrid/queryPutonList",
					"/bookgrid/queryGridBooks",
					"/bookgrid/queryBookRank",
					"/bookgrid/queryShelfBookList",
					"/bookgrid/queryComments"
					);
		registration.setName("checkPageFilter");
		registration.setOrder(2);
		return registration;
	}
}
